package com.example.a10928.weifriend.ui.home;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;

import com.example.a10928.weifriend.R;
import com.example.a10928.weifriend.databinding.ActivityWebViewBinding;
import com.example.a10928.weifriend.http.api.VariableName;
import com.example.a10928.weifriend.ui.BaseFragment;
import com.example.a10928.weifriend.ui.DatabindingBaseActivity;
import com.tencent.smtt.sdk.WebView;

public class WebViewFragment extends BaseFragment {

    public String url = "";
    public String data = "";
    private ActivityWebViewBinding mBinding;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.activity_web_view, container, false);
        initView();
        return mBinding.getRoot();
    }

    private void initView() {

        mBinding.wv.getSettings().setJavaScriptEnabled(true);

        /**
         * LOAD_CACHE_ONLY: 不使用网络，只读取本地缓存数据
         * LOAD_DEFAULT: （默认）根据cache-control决定是否从网络上取数据。
         * LOAD_NO_CACHE: 不使用缓存，只从网络获取数据.
         * LOAD_CACHE_ELSE_NETWORK，只要本地有，无论是否过期，或者no-cache，都使用缓存中的数据。
         */
        mBinding.wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);//不使用缓存，只从网络获取数据.
        //支持屏幕缩放
        mBinding.wv.getSettings().setSupportZoom(true);
        mBinding.wv.getSettings().setBuiltInZoomControls(true);

        mBinding.wv.setWebViewClient(new com.tencent.smtt.sdk.WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(com.tencent.smtt.sdk.WebView webView, String s) {
                mBinding.wv.loadUrl(s);
                return true;
            }
        });

        if (url != null) {
            mBinding.wv.loadUrl(url);
        } else {
            //   data="<!DOCTYPE html>\\n<html lang=\\\"en\\\">\\n<head>\\n    <meta http-equiv=\\\"Content-Type\\\" content=\\\"text\\/html; charset=UTF-8\\\">\\n    <meta name=\\\"viewport\\\" content=\\\"initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no\\\">\\n    <meta name=\\\"format-detection\\\" content=\\\"telephone=no, email=no\\\">\\n    <title>\\u652f\\u4ed8\\u8df3\\u8f6c\\u4e2d....<\\/title>\\n    <style>\\n        body {\\n            font-size: 16px;\\n            text-align:center;\\n            padding-top: 40px;\\n        }\\n    <\\/style>\\n    <script>\\n        window.onload = function () {\\n            var action = document.redirectForm.action;\\n            document.redirectForm.submit();\\n        }\\n    <\\/script>\\n<\\/head>\\n<body>\\n<div class=\\\"block\\\">\\n    <form name = \\\"redirectForm\\\" action=\\\"http:\\/\\/180.168.61.86:27380\\/hpayTransGatewayWeb\\/trans\\/QuickPreDebit.htm\\\" method=\\\"POST\\\" target=\\\"_self\\\">\\n        \\n            <input type=\\\"hidden\\\" name=\\\"accNo\\\" value=\\\"\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"backUrl\\\" value=\\\"http:\\/\\/channel.test.maokung.com\\/r\\/ra\\/channel\\/hanyin\\/unionH5Notify\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"bankCode\\\" value=\\\"\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"currencyCode\\\" value=\\\"156\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"encoding\\\" value=\\\"UTF-8\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"frontUrl\\\" value=\\\"http:\\/\\/channel.test.maokung.com\\/r\\/ra\\/channel\\/hanyin\\/unionH5Return\\/UWTF2018120710565554951\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"goodsDesc\\\" value=\\\"\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"goodsName\\\" value=\\\"\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"hpMerCode\\\" value=\\\"YMHKJZFCSYMH@20180725111551\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"idNo\\\" value=\\\"\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"idType\\\" value=\\\"\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"insCode\\\" value=\\\"80000385\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"insMerchantCode\\\" value=\\\"887581298600447\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"ledger\\\" value=\\\"\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"merReserved\\\" value=\\\"\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"name\\\" value=\\\"\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"nonceStr\\\" value=\\\"c75248498bc64600b546985129893d7b\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"orderAmount\\\" value=\\\"1\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"orderDate\\\" value=\\\"20181207\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"orderNo\\\" value=\\\"UWTF2018120710565554951\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"orderTime\\\" value=\\\"20181207105655\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"orderTimeOut\\\" value=\\\"\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"productCode\\\" value=\\\"10000007\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"requestFlag\\\" value=\\\"web\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"signType\\\" value=\\\"MD5\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"signature\\\" value=\\\"BC51F1CD7C2697BFF317ADAD37516B79\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"telNo\\\" value=\\\"\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"version\\\" value=\\\"1.0.0\\\"> <br\\/\\n        \\n    <\\/form>\\n<\\/div>\\n<\\/body>\\n<\\/html>\"";
            mBinding.wv.loadDataWithBaseURL(null, data, "text/html", "utf-8", null);
        }

    }

    public WebView getView(){
        return mBinding.wv;
    }


}
